const request = require('request');
const config  = require('../config/config');
class Junglescout_login {

    constructor(username,password) {
        this.username = username;
        this.password = password;
    }

    async do(){
        return new Promise((resolve, reject) => {
            let options = {
                url:'https://api.junglescout.com/users/sign_in',
                method:'POST',
                json:true,
                proxy:`http://${config.PROXY.HOST}:${config.PROXY.PORT}`,
                headers: {
                    "content-type": "application/json",
                    "accept": "application/json, text/javascript, */*; q=0.01",
                    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
                    "origin": "chrome-extension://bckjlihkmgolmgkchbpiponapgjenaoa",
                    "cookie":"AWSALB=vgMDox4tZPac9FpmB8/vqn2jqen1oXCnx1NQFkDARcIJCkvcx4xvx6BUW/MZyRqkz5T1fzzA+04Z4nZPy+getie+tLhu+/AzO+cKVWPqLH58fKYokqDE+aNP+wMu;AWSALBCORS=vgMDox4tZPac9FpmB8/vqn2jqen1oXCnx1NQFkDARcIJCkvcx4xvx6BUW/MZyRqkz5T1fzzA+04Z4nZPy+getie+tLhu+/AzO+cKVWPqLH58fKYokqDE+aNP+wMu;"
                },
                body: {email:this.username,password:this.password,mixpanelId:"174b3af1fb566f-03e20749552cdd-594f2116-1fa400-174b3af1fb67f0"}
            }

            request(options,async (err,response,body) => {
                if(err){
                    console.log(`junglescout_login  错误简文:${JSON.stringify(err)}`);
                    reject(err.message)
                }else{
                    let cookie = `${body.data.token}`;
                    console.log(`junglescout_login登录成功1 ${cookie}`);
                    resolve(cookie);
                }
            })
        })
    }

    clearSpecial(str){
        if(!str) return ''
        str = str.replace(/\ +/g,"");
        str = str.replace(/[\r\n]/g,"");
        return str;
    }
}
module.exports = Junglescout_login;
